package com.ihomey.linkuphome.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.ikidou.fragmentBackHandler.BackHandlerHelper;
import com.github.ikidou.fragmentBackHandler.FragmentBackHandler;
import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.base.BaseFragment;

/**
 * Created by Administrator on 2017/4/20.
 */


public class ControlParentFragment extends BaseFragment implements FragmentBackHandler {

    private boolean hasLoaded = false;

    public static ControlParentFragment newInstance(int lightType) {
        ControlParentFragment fragment = new ControlParentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("lightType", lightType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.fragment_base_parent, null);
        view.setBackgroundResource(R.color.white);
        return view;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()&&hasLoaded) {
//            updateControlName();
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        showFragment(ControlFragment.newInstance(getArguments().getInt("lightType")));
    }

    public void showFragment(BaseFragment fragment) {
        if (fragment != null) {
            FragmentTransaction ft = getChildFragmentManager().beginTransaction();
            if (fragment instanceof ControlFragment) {
                ft.replace(R.id.fragment_control_parent, fragment, "Control");
            } else {
                ft.replace(R.id.fragment_control_parent, fragment);
            }
            ft.addToBackStack(null);
            ft.commitAllowingStateLoss();
            hasLoaded=true;
        }
    }

    public void storeLightState() {
        Fragment childFragment = getChildFragmentManager().findFragmentByTag("Control");
        if (childFragment != null && childFragment instanceof ControlFragment) {
            ControlFragment controlFragment = (ControlFragment) childFragment;
            controlFragment.storeLightState();
        }
    }

    public void updateControlName() {
        Fragment childFragment = getChildFragmentManager().findFragmentByTag("Control");
        if (childFragment != null && childFragment instanceof ControlFragment) {
            ControlFragment controlFragment = (ControlFragment) childFragment;
            controlFragment.setUpLightStateView();
        }
    }

    @Override
    public boolean onBackPressed() {
        Fragment childFragment = getChildFragmentManager().findFragmentByTag("Control");
        if (childFragment != null && childFragment.isVisible()) {
           return false;
        }else{
            return BackHandlerHelper.handleBackPress(this);
        }
    }
}
